#{extends 'adminm/frame.html' /}

#{set 'moreScripts'}
<script src="@{'/public/javascripts/jquery-ui-1.8.10.custom.min.js'}" type="text/javascript" charset="utf-8"></script>
<script src="@{'/public/pager/jquery.pager.js'}" type="text/javascript" charset="utf-8"></script>
#{/set}
#{set 'moreStyles'}
<link rel="stylesheet" type="text/css" media="screen" href="@{'/public/pager/Pager.css'}" />
<link rel="stylesheet" type="text/css" media="screen" href="@{'/public/stylesheets/table.scss'}" />
#{/set}
#{set 'activeItem'}7#{/set}

<div class="navigationBar">
	<table>
		<tr>
			<td><img src="@{'/public/images/navigationHouse.gif'}"></td>
			<td><strong>当前位置：</strong></td>
			<td>
				<a href="/admin">首页</a>
				>>
				<a href="#">运价管理</a>
				>>
				<font class="current">公司管理</font>
			</td>
		</tr>
	</table>
</div>

<script language="javascript">

	$(
		function(){
			//初始化创建窗口
			$("#create-form").dialog({autoOpen:false,modal:true,
	        buttons:{提交:function(){createTransPrice();}}
				})

			//初始化更新窗口
			$("#update-form").dialog({autoOpen:false,modal:true,
	        buttons:{提交:function(){updateTransPrice();}}
				})

		}
	);

	//打开创建窗口
	function openCreateDialog(){
		$("#create-form").dialog({width:400});
       $("#create-form").dialog("open");
	}

	//打开修改窗口
	function openUpdateDialog(id){

		$("#companyId-update").val(id);
		
		$.ajax({
			url: "@{admin.Companys.getTransCompany()}",
			data: "companyId="+id,
			type: "GET",
			complete: function(XMLHttpRequest, textStatus, errorThrown) {
				//获取返回内容并进行解析
				var content = XMLHttpRequest.responseText;
				var result = content.split("#");
				for(var i=0;i<result.length;i++)
				{
					var priceResult = result[i].split(":");
					$("#"+priceResult[0]).val(priceResult[1]);
				}
			}
		});

	   $("#update-form").dialog({width:400});
       $("#update-form").dialog("open");
	}

	//刷新页面
	function doSearch() {
		$("#form1").submit();
	}

	//提交创建信息
	function createTransPrice() {
	
		var companyCode = $("#companyCode-create").val();
		var companyName = $("#companyName-create").val();
		var address = $("#address-create").val();
		var phone = $("#phone-create").val();
		var owner = $("#owner-create").val();

		var dataStr = "&companyCode="+companyCode;
		dataStr = dataStr + "&companyName="+companyName;
		dataStr = dataStr + "&address="+address;
		dataStr = dataStr + "&phone="+phone;
		dataStr = dataStr + "&owner="+owner;
		
		$.ajax({
			url: "@{admin.Companys.createTransCompany()}",
			data: dataStr,
			type: "GET",
			complete: function(XMLHttpRequest, textStatus, errorThrown) {
				//获取返回内容并进行解析
				var content = XMLHttpRequest.responseText;
				if("DUPLICATE" == content){
					alert("该公司已经存在，请使用另一公司代码和公司名称");
				}else{
					alert("公司信息创建成功");
					doSearch();
				}
			}
		});
	}

	//提交修改信息
	function updateTransPrice() {
		var companyId = $("#companyId-update").val();
		var companyCode = $("#companyCode-update").val();
		var companyName = $("#companyName-update").val();
		var address = $("#address-update").val();
		var phone = $("#phone-update").val();
		var owner = $("#owner-update").val();

		var dataStr = "companyId="+companyId;
		dataStr = dataStr + "&companyCode="+companyCode;
		dataStr = dataStr + "&companyName="+companyName;
		dataStr = dataStr + "&address="+address;
		dataStr = dataStr + "&phone="+phone;
		dataStr = dataStr + "&owner="+owner;
		
		$.ajax({
			url: "@{admin.Companys.updateTransCompany()}",
			data: dataStr,
			type: "GET",
			complete: function(XMLHttpRequest, textStatus, errorThrown) {
				//获取返回内容并进行解析
				var content = XMLHttpRequest.responseText;
				if("SUCCESS" == content){
					alert("更新成功");
					doSearch();
				}else{
					alert("更新失败");
				}
			}
		});
	}

	//正则表达式校验
	var regex = function(field,regex)
	{
		var targetValue = $(field).val();
		if (targetValue == null || !regex.test(targetValue))
		{
			return false;
		}else{
			return true;
		}
	}

	//必填校验
	var required = function(field ,errTip)
	{
		var targetValue = $(field).val();
		if (targetValue == null || targetValue == "")
		{
			alert(errTip);
			return false;	
		}
		else
		{
			return true;
		}
	}

	//删除 运价
	function removeItem(id) {

		var result = confirm("删除公司信息将同时删除属下的运价，确实要删除吗?")
		if(result == false){
			return false;
		}  
		
		$.ajax({
			url: "@{admin.Companys.deleteTransCompany()}",
			data: "companyId="+id,
			type: "GET",
			complete: function(XMLHttpRequest, textStatus, errorThrown) {
				//获取返回内容并进行解析
				var content = XMLHttpRequest.responseText;
				if("SUCCESS" == content){
					alert("删除成功");
					doSearch();
				}else{
					alert("删除失败");
				}
			}
		});
	}
	
</script>

<div>
	#{if companyList}  
	<table class="common-table">
		<tr>
			<td>公司代码</td>
			<td>公司名称</td>
			<td>公司地址</td>
			<td>联系电话</td>
			<td>负责人</td>
			<td>操作</td>
		</tr>
		#{list items:companyList, as:'company'}
		<tr>
			<td>${company.companyCode}</td>
			<td>${company.companyName}</td>
			<td>${company.address}</td>
			<td>${company.phone}</td>
			<td>${company.owner}</td>
			<td>
				<a class="editer" href="javascript:openUpdateDialog(${company.id})">[修改]</a>&nbsp;
				<a class="editer" href="javascript:removeItem(${company.id})">[删除]</a>&nbsp;
			</td>
		</tr>  		
		#{/list}
	</table>			
	#{/if}
</div>

<div>
<input type="button" value="添加" onclick="javascript:openCreateDialog();">
</div>

<!-- 创建窗口 -->
<div id="create-form" class="ui-widget ui-widget-content ui-corner-all" title="创建运价">
    <form id="create-form">
    	<div class="list_criteria">
		    公司代码：
			<input type="hidden" name="companyId-create" id="companyId-create"/>
		    <input type="text" name="companyCode-create" id="companyCode-create" size="10" /><br/><br/>
		    公司名称：
		    <input type="text" name="companyName-create" id="companyName-create" size="10" />&nbsp;&nbsp;
		    公司地址：
		    <input type="text" name="address-create" id="address-create" size="10" /><br/><br/>
		   联系电话：
		   <input type="text" name="phone-create" id="phone-create" size="10" />&nbsp;&nbsp;
		   负责人：&nbsp;&nbsp;&nbsp;
		   <input type="text" name="owner-create" id="owner-create" size="10"/>
		</div>
    </form>
</div>

<!-- 更新窗口 -->
<div id="update-form" class="ui-widget ui-widget-content ui-corner-all" title="修改公司信息">
	<form id="update-form">
		<div class="list_criteria">
		    公司代码：
			<input type="hidden" name="companyId-update" id="companyId-update"/>
		    <input type="text" name="companyCode-update" id="companyCode-update" size="10" /><br/><br/>
		    公司名称：
		    <input type="text" name="companyName-update" id="companyName-update" size="10" />&nbsp;&nbsp;
		    公司地址：
		    <input type="text" name="address-update" id="address-update" size="10" /><br/><br/>
		   联系电话：
		   <input type="text" name="phone-update" id="phone-update" size="10" />&nbsp;&nbsp;
		   负责人：&nbsp;&nbsp;&nbsp;
		   <input type="text" name="owner-update" id="owner-update" size="10"/>
		</div>
	</form> 
</div>

<div>
	#{form @admin.Companys.indexCompany(), name:'form1', id:'form1'}
  	#{/form}              
</div>

</div>
